package org.hxyjs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.hxyjs.aop.SystemMail;
import org.hxyjs.dto.DTO;


import org.hxyjs.entity.UserSchemeRecord;
import org.hxyjs.page.PageInfo;

import org.hxyjs.service.UserSchemeRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;


@CrossOrigin
@Controller //@Controller注解是用spring容器生成 对象 默认对象名 userSchemRecordController
@RestController //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /userSchemRecordController
public class UserSchemeRecordController{
	@Resource
	UserSchemeRecordService userSchemRecordService;
	@GetMapping("/userSchemeRecords")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<UserSchemeRecord> getAllUserSchemeRecords() throws Exception{
		System.out.println("查询全部");
		return userSchemRecordService.getAllUserSchemeRecords();
	}

	@GetMapping("/userSchemeRecords/{usrno}")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
	public DTO<UserSchemeRecord> getUserSchemeRecordByUno(@PathVariable("usrno") String usrno) throws Exception{
		System.out.println(usrno + "查询成功");
		return userSchemRecordService.getUserSchemeRecordByUno(usrno);
	}
	//@SystemMail(op = "UserSchemeRecord:addUserSchemeRecord",queueName = "family.mail",module = "用户管理",methods = "用户注册")
	@PostMapping("/userSchemeRecords")
	@ResponseBody
	public DTO<UserSchemeRecord> addUserSchemeRecordByUno(@RequestBody UserSchemeRecord userSchemRecord) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加成功");
		return userSchemRecordService.addUserSchemeRecordByUno(userSchemRecord);
	}

	@DeleteMapping("/userSchemeRecords/{usrno}")
	@ResponseBody
	public DTO<UserSchemeRecord> deleteUserSchemeRecord(@PathVariable("usrno") String usrno) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println(usrno+"删除成功");
		return userSchemRecordService.deleteUserSchemeRecord(usrno);
	}

	@RequestMapping("/getUserSchemeRecordsWithOrders/{usrno}")
	@ResponseBody
	public DTO<UserSchemeRecord> getUserSchemeRecordsWithOrders(@PathVariable("usrno") String usrno,HttpServletRequest request,HttpServletResponse response) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询学生选课111");
		DTO<UserSchemeRecord> dto = userSchemRecordService.getUserSchemeRecordsWithOrders(usrno);
		saveSelectCourseCookieProcess(dto.getT(),request,response);
		return  dto;
	}
	@PatchMapping("/userSchemeRecords")
	@ResponseBody
	public DTO<UserSchemeRecord> updateUserSchemeRecord(@RequestBody UserSchemeRecord userSchemRecord) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改成功");
		return userSchemRecordService.updateUserSchemeRecord(userSchemRecord);
	}
	@RequestMapping("/userSchemeRecords/pageInfo")
	@ResponseBody
	public DTO<UserSchemeRecord> getUserSchemeRecordsByPage(PageInfo pageInfo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询分页");
		return userSchemRecordService.getUserSchemeRecordsByPage(pageInfo);
	}
	public DTO<UserSchemeRecord> saveSelectCourseCookieProcess(UserSchemeRecord userSchemRecord, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException, UnsupportedEncodingException {
		//cookie是以key value形式存放在浏览器的文本
		DTO dto = new DTO(200,"cookie保存成功");
//		if(userSchemRecord!=null&&userSchemRecord.getUno()!=null) {
//			ObjectMapper objectMapper = new ObjectMapper();
//			Optional<List<OrderForm>> orderFormsOp = Optional.ofNullable(userSchemRecord.getOrderForms());
//			List<OrderForm> orderForms = null;
//			if (orderFormsOp.isPresent()&&orderFormsOp.get().size()>0) {
//				orderForms = new ArrayList<OrderForm>(userSchemRecord.getOrderForms());
//
//				for (OrderForm orderForm : userSchemRecord.getOrderForms()) {
//					Cookie orderFormsCookie = new Cookie("userSchemRecord" + userSchemRecord.getUno() + "_" + orderForm.getUrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderForm), "UTF-8"));
//
//					//userSchemRecordCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//					orderFormsCookie.setPath("/");
//					orderFormsCookie.setDomain("localhost");
//					orderFormsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//					response.addCookie(orderFormsCookie);
//
//				}
//			}
//			userSchemRecord.setOrderForms(null);
//			Cookie userSchemRecordCookie = new Cookie("userSchemRecord" + userSchemRecord.getUno(), URLEncoder.encode(objectMapper.writeValueAsString(userSchemRecord), "UTF-8"));
//			userSchemRecordCookie.setPath("/");
//			userSchemRecordCookie.setDomain("localhost");
//			userSchemRecordCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(userSchemRecordCookie);
//			userSchemRecord.setOrderForms(orderForms);
//		}else{
//			dto.setCode(406);
//			dto.setMsg("缺少必要的参数，非法操作");
//		}
		return dto;
	}



	@RequestMapping("/activate/{usrno}")
	@ResponseBody
	public DTO<UserSchemeRecord> activate(@PathVariable("usrno") String usrno) throws Exception{

		return userSchemRecordService.activate(usrno);
	}

	@SystemMail(op = "UserSchemeRecord:lettory",queueName = "family.mail",module = "中奖",methods = "中奖信息")
	@RequestMapping("/lettory/{usrno}/{giftName}")
	@ResponseBody
	public DTO<UserSchemeRecord> lettory(@PathVariable("usrno") String usrno,@PathVariable("giftName")String giftName) throws Exception{

		return userSchemRecordService.lettory(usrno,giftName);
	}



}
